//
//  MControlViewController.m
//  Mask
//
//  Created by 郭子一 on 2020/10/2.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MControlViewController.h"
#import "MControlDeviceTypeCell.h"
#import "MControlSubView.h"
#import "MControlCustomzButtonCell.h"
@interface MControlViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *deviceImage; // 设备图片
@property (nonatomic, strong) UICollectionView *stateCollectionView; // 模式列表
@property (nonatomic, strong) MControlSubView *pressureView; // 压力调节
@property (nonatomic, strong) MControlSubView *boostView; // 助力调节
@property (nonatomic, strong) UICollectionView *customizeButtonCollectionView; // 自定义按钮视图

@end

@implementation MControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeUI];
}

- (void)makeUI
{
    UIScrollView *bgScrollview = [[UIScrollView alloc] init];
    bgScrollview.showsVerticalScrollIndicator = NO;
//    bgScrollview.contentSize = CGSizeMake(kGScreenWidth, 812);
    [self.view addSubview:bgScrollview];
    [bgScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kGScreenWidth);
        make.top.equalTo(self.view);
        make.height.mas_equalTo(kGScreenHeight);
    }];
    
    if (@available(iOS 11.0, *)) {
        bgScrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    UIView *headerBgView = [[UIView alloc] init];
    headerBgView.frame = CGRectMake(0, 0, 0, 300);
    [bgScrollview addSubview:headerBgView];
    [headerBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.top.equalTo(self.view);
        make.height.mas_equalTo(300);
    }];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)RGB_VALUE(0x0191FE).CGColor, (__bridge id)RGB_VALUE(0x00C3FE).CGColor, (__bridge id)RGB_VALUE(0xFFFFFF).CGColor];
    //       gradientLayer.locations = @[@0.3, @5.5, @10.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    gradientLayer.frame = CGRectMake(0, 0, kGScreenWidth, 300);
    [headerBgView.layer addSublayer:gradientLayer];
    
    self.navTitleLab = [[UILabel alloc] init];
    self.navTitleLab.font = JSS_FONT_6;
    self.navTitleLab.textAlignment = NSTextAlignmentCenter;
    self.navTitleLab.textColor = RGB_VALUE(0xFFFFFF);
    self.navTitleLab.text = @"控制面板";
    [bgScrollview addSubview:self.navTitleLab];
    [self.navTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.centerX.offset(0);
        make.height.mas_equalTo(22);
        make.top.offset(64);
    }];
    
    self.deviceImage = [[UIImageView alloc] init];
    self.deviceImage.backgroundColor = UIColor.cyanColor;
    [bgScrollview addSubview:self.deviceImage];
    [self.deviceImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(self.navTitleLab.mas_bottom).offset(24);
        make.width.mas_equalTo(145);
        make.height.mas_equalTo(102);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.stateCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.stateCollectionView.delegate = self;
    self.stateCollectionView.dataSource = self;
    self.stateCollectionView.backgroundColor = UIColor.clearColor;
    self.stateCollectionView.showsHorizontalScrollIndicator = NO;
    [self.stateCollectionView registerClass:[MControlDeviceTypeCell class] forCellWithReuseIdentifier:@"collectionViewId"];
    [bgScrollview addSubview:self.stateCollectionView];
    [self.stateCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.deviceImage.mas_bottom).offset(13);
        make.width.mas_equalTo(kGScreenWidth);
        make.height.mas_equalTo(140);
    }];
    
    UIView *controlView = [[UIView alloc] init];
    controlView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    controlView.layer.cornerRadius = 10;
    controlView.layer.shadowColor = [UIColor colorWithRed:93/255.0 green:102/255.0 blue:147/255.0 alpha:0.15].CGColor;
    controlView.layer.shadowOffset = CGSizeMake(0,2);
    controlView.layer.shadowOpacity = 1;
    controlView.layer.shadowRadius = 8;
    [bgScrollview addSubview:controlView];
    [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.stateCollectionView.mas_bottom).offset(10);
        make.width.mas_equalTo(kGScreenWidth-30);
        make.height.mas_equalTo(180);
        make.left.offset(15);
    }];
    
    UILabel *breathFollowLabel = [[UILabel alloc] init];
    breathFollowLabel.text = @"呼吸跟随";
    breathFollowLabel.font = JSS_FONT_3;
    breathFollowLabel.textColor = RGB_VALUE(0xB6C1C7);
    [controlView addSubview:breathFollowLabel];
    [breathFollowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-106);
        make.top.offset(15);
        make.width.mas_equalTo(52);
        make.height.mas_equalTo(18);
    }];
    
    UISwitch *breathLightSwitch = [[UISwitch alloc] init];
    [breathLightSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    breathLightSwitch.on = YES;
    breathLightSwitch.onTintColor = RGB_VALUE(0x00C3FE);
    breathLightSwitch.transform = CGAffineTransformMakeScale(0.7, 0.5);
    [controlView addSubview:breathLightSwitch];
    [breathLightSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-55);
        make.centerY.equalTo(breathFollowLabel).offset(-3);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(40);
    }];
    
    UILabel *breathSubLabel = [[UILabel alloc] init];
    breathSubLabel.text = @"恒流";
    breathSubLabel.font = JSS_FONT_3;
    breathSubLabel.textColor = RGB_VALUE(0x2D3033);
    [controlView addSubview:breathSubLabel];
    [breathSubLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.equalTo(breathFollowLabel);
        make.width.mas_equalTo(26);
        make.height.mas_equalTo(18);
    }];
    
    self.pressureView = [[MControlSubView alloc] init];
    [controlView addSubview:self.pressureView];
    [self.pressureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(breathFollowLabel.mas_bottom).offset(30);
        make.height.mas_equalTo(36);
        make.left.right.offset(0);
    }];
    
    self.boostView = [[MControlSubView alloc] init];
    [controlView addSubview:self.boostView];
    [self.boostView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pressureView.mas_bottom).offset(23);
        make.height.mas_equalTo(36);
        make.left.right.offset(0);
    }];
    
    UIView *customizeView = [[UIView alloc] init];
    customizeView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    customizeView.layer.cornerRadius = 10;
    customizeView.layer.shadowColor = [UIColor colorWithRed:93/255.0 green:102/255.0 blue:147/255.0 alpha:0.15].CGColor;
    customizeView.layer.shadowOffset = CGSizeMake(0,2);
    customizeView.layer.shadowOpacity = 1;
    customizeView.layer.shadowRadius = 8;
    [bgScrollview addSubview:customizeView];
    [customizeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(controlView.mas_bottom).offset(10);
        make.width.mas_equalTo(kGScreenWidth-30);
        make.height.mas_equalTo(170);
        make.left.offset(15);
    }];
    
    UILabel *customizeLabel = [[UILabel alloc] init];
    customizeLabel.text = @"按钮定义";
    customizeLabel.font = JSS_FONT_3;
    customizeLabel.textColor = RGB_VALUE(0x2D3033);
    [customizeView addSubview:customizeLabel];
    [customizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.offset(15);
        make.width.mas_equalTo(52);
        make.height.mas_equalTo(18);
    }];
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [editButton setTitleColor:RGB_VALUE(0x878F99) forState:UIControlStateNormal];
    [editButton.titleLabel setFont:JSS_FONT_3];
    [editButton addTarget:self action:@selector(editButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [customizeView addSubview:editButton];
    [editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(customizeLabel);
        make.right.offset(0);
        make.width.mas_equalTo(56);
        make.height.mas_equalTo(35);
    }];
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc] init];
    layout1.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout1.minimumLineSpacing = 0;
    layout1.minimumInteritemSpacing = 0;
    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.customizeButtonCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout1];
    self.customizeButtonCollectionView.delegate = self;
    self.customizeButtonCollectionView.dataSource = self;
    self.customizeButtonCollectionView.backgroundColor = UIColor.clearColor;
    [self.customizeButtonCollectionView registerClass:[MControlCustomzButtonCell class] forCellWithReuseIdentifier:@"MControlCustomzButtonCell"];
    [customizeView addSubview:self.customizeButtonCollectionView];
    [self.customizeButtonCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customizeLabel.mas_bottom).offset(29);
        make.left.right.offset(0);
        make.height.mas_equalTo(84);
    }];
    
    CGFloat offsets = (kGScreenWidth - 30)/4;
    UIImageView *firstArrowImg = [[UIImageView alloc] init];
    firstArrowImg.image = [UIImage imageNamed:@"homeArrow"];
    [customizeView addSubview:firstArrowImg];
    [firstArrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(offsets - 13);
        make.bottom.offset(-37);
        make.width.height.mas_equalTo(26);
    }];
    UIImageView *secondArrowImg = [[UIImageView alloc] init];
    secondArrowImg.image = [UIImage imageNamed:@"homeArrow"];
    [customizeView addSubview:secondArrowImg];
    [secondArrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(offsets*2-13);
        make.bottom.offset(-37);
        make.width.height.mas_equalTo(26);
    }];
    UIImageView *thirdArrowImg = [[UIImageView alloc] init];
    thirdArrowImg.image = [UIImage imageNamed:@"homeArrow"];
    [customizeView addSubview:thirdArrowImg];
    [thirdArrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(offsets*3-13);
        make.bottom.offset(-37);
        make.width.height.mas_equalTo(26);
    }];
    
    [bgScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        // 注意点2：
        make.bottom.equalTo(customizeView.mas_bottom).offset(62);
    }];
}

- (void)switchValueChanged:(UISwitch *)sender
{
    
}

- (void)editButtonClick
{
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.stateCollectionView) {
        return 3;
    } else if (collectionView == self.customizeButtonCollectionView) {
        return 4;
    }
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.stateCollectionView) {
        return CGSizeMake(265, 140);
    } else if (collectionView == self.customizeButtonCollectionView) {
        return CGSizeMake((kGScreenWidth-30)/4, 84);
    }
    return CGSizeZero;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.stateCollectionView) {
        MControlDeviceTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewId" forIndexPath:indexPath];
        
        return cell;
    } else if (collectionView == self.customizeButtonCollectionView) {
        MControlCustomzButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MControlCustomzButtonCell" forIndexPath:indexPath];
        
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.stateCollectionView) {
        
    } else if (collectionView == self.customizeButtonCollectionView) {
        
    }
}

@end
